package com.qmrssreader.system;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;

public class DBAdapter {

	public static final String TAG = "DBAdapter";

	public static final String KEY_NAME = "name";
	public static final String KEY_LINK = "link";

	private DatabaseHelper mDbHelper;
	private SQLiteDatabase mDB;

	private static final String DATABASE_NAME = "Database_RssLink";
	private static final String DATABASE_TABLE = "rsslink";
	private static final int DATABASE_VERSION = 2;
	private static final String DATABASE_CREATE = "" + "CREATE TABLE "
			+ DATABASE_TABLE
			+ " (name TEXT PRIMARY KEY  NOT NULL , link VARCHAR)";

	private final Context mContext;

	private static class DatabaseHelper extends SQLiteOpenHelper {

		public DatabaseHelper(Context context, String name,
				CursorFactory factory, int version) {
			super(context, name, factory, version);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
			db.execSQL(DATABASE_CREATE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
			Log.i(TAG, "Upgrading DB");
			db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
			onCreate(db);
		}
	}

	public DBAdapter(Context ctx) {
		this.mContext = ctx;
	}

	public DBAdapter open() {
		mDbHelper = new DatabaseHelper(mContext, DATABASE_NAME, null,
				DATABASE_VERSION);
		mDB = mDbHelper.getWritableDatabase();

		return this;
	}

	public void close() {
		mDbHelper.close();
	}

	public long createRss(String name, String link) {
		ContentValues inititalValues = new ContentValues();
		inititalValues.put(KEY_NAME, name);
		inititalValues.put(KEY_LINK, link);
		return mDB.insert(DATABASE_TABLE, null, inititalValues);
	}

	public boolean deleteRss(String rowName) {
		return mDB
				.delete(DATABASE_TABLE, KEY_NAME + "='" + rowName + "'", null) > 0;
	}

	public boolean deleteAllRss() {
		return mDB.delete(DATABASE_TABLE, "1", null) > 0;
	}

	public Cursor getAllRss() {
		return mDB.query(DATABASE_TABLE, new String[] { KEY_NAME }, null, null,
				null, null, null);
	}

	public Cursor getAllRss1() {
		return mDB.query(DATABASE_TABLE, null, null, null, null, null, KEY_NAME);		
	}

	public Cursor getAllRss2() {
		return mDB.query(DATABASE_TABLE, null, KEY_NAME + " like 'QM%'", null,
				null, null, KEY_NAME);
	}

	public Cursor getOneRss(String pname) {
		return mDB.query(DATABASE_TABLE, new String[] { KEY_NAME, KEY_LINK },
				KEY_NAME + "='" + pname + "'", null, null, null, null);
	}

	public boolean existsURL(String url) {
		Cursor cur = mDB
				.query(DATABASE_TABLE, new String[] { KEY_NAME, KEY_LINK },
						KEY_LINK + "='" + url + "'", null, null, null, null);
		return cur.getCount() > 0;
	}

	public boolean existsName(String name) {
		Cursor cur = mDB
				.query(DATABASE_TABLE, new String[] { KEY_NAME, KEY_LINK },
						KEY_NAME + "='" + name + "'", null, null, null, null);
		return cur.getCount() > 0;
	}
	/*
	 * public int updateUser(String newName, String[] whereName){ ContentValues
	 * cnt=new ContentValues(); cnt.put(KEY_NAME, newName); return
	 * mDB.update(DATABASE_TABLE, cnt, KEY_NAME+"=?",whereName); }
	 */

	public int updateRss(String newName, String link, String pNameKey) {
		ContentValues cnt = new ContentValues();
		cnt.put(KEY_NAME, newName);
		cnt.put(KEY_LINK, link);
		return mDB.update(DATABASE_TABLE, cnt,
				KEY_NAME + "='" + pNameKey + "'", null);
	}
}
